package com.miaoji.controller;

import com.miaoji.domain.dto.StockDto;
import com.miaoji.domain.po.Stock;
import com.miaoji.service.StockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/stock")
public class StockManagementController {
    @Autowired
    StockService stockService;
    @PostMapping("/select")
    List<StockDto> StockSelect(@RequestBody int cloth_id){
        List<Stock> stocks = stockService.StockSelect(cloth_id);
        ArrayList<StockDto> returnMap = new ArrayList<>();
        for (Stock stock : stocks) {
            StockDto stockDto = new StockDto();
            String imgUrl = stock.getImgUrl();
            byte[] imageAsBytes = getImageAsBytes(imgUrl);
            int lastIndex = imgUrl.lastIndexOf("/");
            String fileName = imgUrl.substring(lastIndex + 1);
            stockDto.setId(stock.getId());
            stockDto.setClothId(stock.getClothId());
            stockDto.setSizeId(stock.getSizeId());
            stockDto.setColorId(stock.getColorId());
            stockDto.setStock(stock.getStock());
            stockDto.setImageBytes(imageAsBytes);
            stockDto.setImgUrl(imgUrl);
            stockDto.setFileName(fileName);
            stockDto.setComment2(stock.getComment2());
            stockDto.setComment3(stock.getComment3());
        }
        return returnMap;
    }
    @PostMapping("/insert")
    void StockInsert(@RequestBody StockDto stockDto){
        this.saveImage(stockDto.getFileName(), stockDto.getImageBytes());
        Stock stock=convertToStock(stockDto);
        stockService.StockInsert(stock);
    }



    @PostMapping("/delete")
    void StockDelete(@RequestBody StockDto stockDto){
        Stock stock = convertToStock(stockDto);
        stockService.StockDelete(stock);
    }
    @PostMapping("update")
    void StockUpdate(@RequestBody StockDto stockDto){
        this.saveImage(stockDto.getFileName(), stockDto.getImageBytes());
        Stock stock = convertToStock(stockDto);
        stockService.StockUpdate(stock);
    }
    public void saveImage(String fileName,byte[] imageBytes) {
        String filePath = "img/" + fileName; // Assuming img directory exists in project root
        try {
            FileOutputStream fos = new FileOutputStream(filePath);
            fos.write(imageBytes);
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
            // Handle exception
        }
    }

    public byte[] getImageAsBytes(String imgUrl) {
        try {
            String filePath = "img/" + imgUrl; // Assuming imgUrl is the file name
            File file = new File(filePath);
            InputStream inputStream = new FileInputStream(file);
            byte[] bytes = FileCopyUtils.copyToByteArray(inputStream);
            inputStream.close();
            return bytes;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
    private Stock convertToStock(StockDto stockDto) {
        Stock stock = new Stock();
        stock.setClothId(stockDto.getClothId());
        stock.setSizeId(stockDto.getSizeId());
        stock.setColorId(stockDto.getColorId());
        stock.setStock(stockDto.getStock());
        stock.setImgUrl(stockDto.getImgUrl());
        stock.setComment2(stockDto.getComment2());
        stock.setComment3(stockDto.getComment3());
        return stock;
    }
}
